package com.xiaocz.hblsubstitutedriving.activities.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Inputtips.InputtipsListener;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.xiaocz.common.widgets.recycler.RecyclerViewAdapter;
import com.xiaocz.hblsubstitutedriving.R;
import com.xiaocz.hblsubstitutedriving.adapter.SearchListAdapter;
import com.xiaocz.hblsubstitutedriving.base.BaseActivity;
import com.xiaocz.hblsubstitutedriving.model.PositionEntity;
import com.xiaocz.hblsubstitutedriving.utils.Constants;
import com.xiaocz.hblsubstitutedriving.widget.ClearTextIconEditText;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 地名搜索
 *
 * @author xcz
 */
public class SearchDestinationActivity extends BaseActivity implements TextWatcher,
        InputtipsListener {


    @BindView(R.id.ed_address)
    ClearTextIconEditText edAddress;
    @BindView(R.id.recycler_view)
    public RecyclerView mRecyclerView;

    SearchListAdapter searchListAdapter;

    public static void show(Context context) {
        Intent intent = new Intent(context, SearchDestinationActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutSourceId() {
        return R.layout.activity_destination;
    }

    @Override
    protected void initWidgets() {
        super.initWidgets();
        edAddress.addTextChangedListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        searchListAdapter = new SearchListAdapter(new RecyclerViewAdapter.AdapterListenerImpl<PositionEntity>() {
            @Override
            public void onItemClick(RecyclerViewAdapter.ViewHolder<PositionEntity> holder, PositionEntity data) {
                super.onItemClick(holder, data);
                if (data != null) {
                    Intent intent = new Intent();
                    intent.putExtra("SEARCH_ADDRESS", data);
                    intent.putExtra("LAT",data.getLatitue());
                    intent.putExtra("LNG",data.getLongitude());
                    intent.putExtra("CITY",data.getCity());
                    intent.putExtra("ADDRESS",data.getAddress());
                    intent.putExtra("NAME",data.getName());
                    setResult(2, intent);
                    finish();
                }
            }
        });
        mRecyclerView.setAdapter(searchListAdapter);
    }


    @OnClick({R.id.destination_back, R.id.tv_my_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.destination_back://返回
                finish();
                break;
            case R.id.tv_my_location://我的位置
                break;
        }
    }

    @Override
    public void afterTextChanged(Editable arg0) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String newText = s.toString().trim();
        // 第二个参数默认代表全国，也可以为城市区号
        String cityCode = null;
        if (Constants.getLocation() != null) {
            cityCode = Constants.getLocation().getCityCode();
        }
        if (newText.length() > 0) {
            InputtipsQuery inputQuery = new InputtipsQuery(newText, cityCode);
            inputQuery.setCityLimit(false);//isLimit - true，城市限制；false，输入文字是权重词汇时，全国搜索。默认为false。
            Inputtips inputTips = new Inputtips(SearchDestinationActivity.this,
                    inputQuery);
            inputTips.requestInputtipsAsyn();
            inputTips.setInputtipsListener(this);
        } else {
            searchListAdapter.clear();
        }
    }

    @Override
    public void onGetInputtips(List<Tip> tipList, int rCode) {
        if (rCode == 1000) {
            List<PositionEntity> mPositionEntities = new ArrayList<>();
            for (Tip tip : tipList) {
                LatLonPoint latLonPoint = tip.getPoint();
                if (latLonPoint != null) {
                    mPositionEntities.add(new PositionEntity(latLonPoint
                            .getLatitude(), latLonPoint.getLongitude(), tip.getName(),
                            tip.getDistrict(), tip.getAddress()));
                }
            }
            searchListAdapter.replace(mPositionEntities);
        }
    }
}
